﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sino.CapacityCloud.WebApi;
using Sino.CapacityCloud.WebApi.Client;
using Plugin.Connectivity.Abstractions;
using Sino.WebApi.Framework.Primitives;

namespace Sino.CapacityCloud.Web
{

    public class Startup
    {

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var client = new CapacityCloudRestfulApiClient(new NullIConnectivity(), new WebApi.Client.ConfigurationOptions()
            {
                BaseUri = "http://localhost:5003/api/",
                AppSecrect = "FnngEX4S3aB",
                AppId = "82hegw5uhqtcx",
                AccessTimeoutRetries = 1,
                ApiVersion = new Version(1, 0),
                Platform = OsCode.Web,
                DefaultTimeoutInMillionSeconds = 10000
            });

            services.AddSingleton<IUserService, CapacityCloudRestfulApiClient>(x =>
            {
                return client;
            });

            services.AddSingleton<IOrderService, CapacityCloudRestfulApiClient>(x =>
            {
                return client;
            });
            services.AddSingleton<IRoleService, CapacityCloudRestfulApiClient>(x =>
            {
                return client;
            });

            services.AddMvc();
            services.AddSwaggerGen();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseCors(builder =>
                builder.AllowAnyOrigin()
                    .AllowAnyHeader()
            );

            // Enable middleware to serve generated Swagger as a JSON endpoint
            app.UseSwagger();

            // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
            app.UseSwaggerUi();
        }

    }

}